﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL.Helper;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.UserControls
{
    public partial class Account : System.Web.UI.UserControl
    {
        UserBl userBl = new UserBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                User user = userBl.GetById(GsjUserHelper.UserId);
                lblUsername.Text = user.Username;
                tbxNickname.Text = user.NickName;
                tbxEmail.Text = user.Email;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            User user = userBl.GetById(GsjUserHelper.UserId);
            user.NickName = tbxNickname.Text.Trim();
            user.Email = tbxEmail.Text.Trim();

            string oldPwd = tbxOld.Text.Trim();
            string newPwd = tbxNew.Text.Trim();
            string newPwd2 = tbxNewAgain.Text.Trim();
            if (!string.IsNullOrEmpty(newPwd))
            {
                if (string.IsNullOrEmpty(oldPwd))
                {
                    AlertHelper.ShowError("原密码必须填写", this.Page);
                    return;
                }
                else
                {
                    if (oldPwd != user.Password)//密码不正确
                    {
                        AlertHelper.ShowError("密码不正确", this.Page);
                        return;
                    }

                    else
                    {
                        if (newPwd != newPwd2)//两次不一致
                        {
                            AlertHelper.ShowError("两次输入的新密码不一致", this.Page);
                            return;
                        }
                        else
                        {
                            //ok 
                            user.Password = newPwd;
                        }
                    }
                }
            }

            userBl.UpdateUser(user);
            MessageHelper.RegShowJSAjax(btnSave, "修改成功", this.Page);

        }
    }
}